﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietImage.Models;
using System.IO;

namespace VietImage.Controllers
{
    public class ImageController : Controller
    {
        //
        // GET: /Image/

        public void GetLanguage()
        {
            HttpCookie languageCookie = null;
            if (languageCookie == null)
            {
                languageCookie = new HttpCookie("Language");
                languageCookie.Value = "2";
            }
            languageCookie.Expires = DateTime.Now.AddMonths(1);
            //Response.SetCookie(languageCookie);
            ViewData["Language"] = int.Parse(languageCookie.Value);
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public String UploadImage(IEnumerable<HttpPostedFileBase> files)
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                if (!VietImage.Helpers.ControllerHelper.HasFile(Request.Files[i])) continue;
                string filename = Path.GetFileName(Request.Files[i].FileName);
                Request.Files[i].SaveAs(Path.Combine(Server.MapPath("~/App_Data"), filename));
            }
            VietImage.Helpers.ControllerHelper.GetLanguage(ViewData, Request, Response);
            return "Success!";
        }

        [HttpPost]
        public ActionResult Detail(int id = 0)
        {
            Image img = Image.GetImage(id);
            return PartialView("Detail", img);
        }

        public ActionResult Upload()
        {
            GetLanguage();
            ViewData["CategoryList"] = VietImage.Models.Category.GetListAvailableParentCategories();
            if (Request["cat"] != null)
            {
                ViewData["SelectedCategory"] = VietImage.Models.Category.GetCategory(int.Parse(Request["cat"]));
            }
            return View("Upload");
        }

        public ActionResult Display(int id)
        {
            Image img = Image.GetImage(id);
            ImageResult result = new ImageResult("~\\Content\\Images\\noimage.png", "image/jpeg");
            if (img != null)
                result = new ImageResult(img.Source.ToArray(), "image/jpeg");
            return result;
        }

        public ActionResult Category(int id)
        {
            Category c = VietImage.Models.Category.GetCategory(id);
            ImageResult result = new ImageResult("~\\Content\\Images\\noimage.png", "image/jpeg");
            if (c.PrimaryImage != null)
                result = new ImageResult(c.PrimaryImage.ToArray(), "image/jpeg");
            return result;
        }

      public class ImageResult : ActionResult
        {
            public String ContentType { get; set; }
            public byte[] ImageBytes { get; set; }
            public String SourceFilename { get; set; }

            //This is used for times where you have a physical location
            public ImageResult(String sourceFilename, String contentType)
            {
                SourceFilename = sourceFilename;
                ContentType = contentType;
            }

            //This is used for when you have the actual image in byte form
            //  which is more important for this post.
            public ImageResult(byte[] sourceStream, String contentType)
            {
                ImageBytes = sourceStream;
                ContentType = contentType;
            }

            public override void ExecuteResult(ControllerContext context)
            {
                var response = context.HttpContext.Response;
                response.Clear();
                response.Cache.SetCacheability(HttpCacheability.NoCache);
                response.ContentType = ContentType;

                //Check to see if this is done from bytes or physical location
                //  If you're really paranoid you could set a true/false flag in
                //  the constructor.
                if (ImageBytes != null)
                {
                    var stream = new MemoryStream(ImageBytes);
                    stream.WriteTo(response.OutputStream);
                    stream.Dispose();
                }
                else
                {
                    response.TransmitFile(SourceFilename);
                }
            }

        }
    }
}
